{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #位 1 的个数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: hammingWeight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #位 1 的个数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>编写一个函数，输入是一个无符号整数（以二进制串的形式），返回其二进制表达式中数字位数为 '1' 的个数（也被称为&nbsp;<a href=\"http://en.wikipedia.org/wiki/Hamming_weight\" target=\"_blank\">汉明重量</a>).）。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>请注意，在某些语言（如 Java）中，没有无符号整数类型。在这种情况下，输入和输出都将被指定为有符号整数类型，并且不应影响您的实现，因为无论整数是有符号的还是无符号的，其内部的二进制表示形式都是相同的。</li>\n",
    "\t<li>在 Java 中，编译器使用 <a href=\"https://baike.baidu.com/item/二进制补码/5295284\">二进制补码</a> 记法来表示有符号整数。因此，在上面的&nbsp;<strong>示例 3&nbsp;</strong>中，输入表示有符号整数 <code>-3</code>。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 11 (控制台输入 00000000000000000000000000001011)\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>输入的二进制串 <code><strong>00000000000000000000000000001011</strong>&nbsp;中，共有三位为 '1'。</code>\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 128 (控制台输入 00000000000000000000000010000000)\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>输入的二进制串 <strong>00000000000000000000000010000000</strong>&nbsp;中，共有一位为 '1'。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 4294967293 (控制台输入 11111111111111111111111111111101，部分语言中 n = -3）\n",
    "<strong>输出：</strong>31\n",
    "<strong>解释：</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中，共有 31 位为 '1'。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>输入必须是长度为 <code>32</code> 的 <strong>二进制串</strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p>注意：本题与主站 191 题相同：<a href=\"https://leetcode-cn.com/problems/number-of-1-bits/\">https://leetcode-cn.com/problems/number-of-1-bits/</a></p>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [er-jin-zhi-zhong-1de-ge-shu-lcof](https://leetcode.cn/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [er-jin-zhi-zhong-1de-ge-shu-lcof](https://leetcode.cn/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['00000000000000000000000000001011', '00000000000000000000000010000000', '11111111111111111111111111111101']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt = 0\n",
    "        \n",
    "        while n:\n",
    "            cnt += n & 1\n",
    "            n >>= 1\n",
    "            \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = 0\n",
    "        while n:\n",
    "            n &= n-1\n",
    "            ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n & 1\n",
    "            n >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            n -= n & (-n)\n",
    "            res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        result = 0\n",
    "        s = bin(n)[2:]\n",
    "        for i in s:\n",
    "            if i == '1':\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = 0\n",
    "        while n:\n",
    "            n &= n - 1\n",
    "            ret += 1\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt = 0\n",
    "        while n:\n",
    "            n = n & (n-1) # 消去n最右边的1\n",
    "            cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        b = bin(n)\n",
    "        s = str(b)\n",
    "        cnt = 0\n",
    "        for i in s:\n",
    "            if i == '1':\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        flag = 0x1\n",
    "        count = 0\n",
    "        while flag <= n:\n",
    "            if (flag & n) != 0:\n",
    "                count += 1\n",
    "            flag <<= 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        # 暴力\n",
    "        # cnt = 0\n",
    "        # while n:\n",
    "        #     cnt += n&1\n",
    "        #     n >>= 1\n",
    "        # return cnt\n",
    "\n",
    "\n",
    "        # n&(n-1) 把最右边的1变为0\n",
    "        cnt = 0\n",
    "        while n:\n",
    "            cnt += 1\n",
    "            n &= n-1\n",
    "        return cnt\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "\n",
    "        ## &1：末尾判断，0&1=0，1&1=1\n",
    "        res = 0\n",
    "        while n > 0:\n",
    "            res += n&1\n",
    "            n >>= 1 #二进制右移（即截断右侧一位）\n",
    "        return res\n",
    "\n",
    "        ## &(n-1)：查1，n-1最右边1置为0且后续全置为1，和n&后相当于消去最右边的1\n",
    "        res = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = 0\n",
    "        while n:\n",
    "            n &= n - 1\n",
    "            ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += 1\n",
    "            n &= n - 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count=0\n",
    "        # while n:\n",
    "        #     count+=n&1\n",
    "        #     n=n>>1\n",
    "        while n:\n",
    "            n=n&(n-1)\n",
    "            count +=1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            n = n & (n-1)\n",
    "            res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += 1\n",
    "            n = n & (n-1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        return str(bin(n)).count('1')\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = sum([1 for i in range(32) if n & (1 << i)])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            if n&1:\n",
    "                res+=1\n",
    "            n>>=1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n:\n",
    "            ans += n & 1\n",
    "            n >>= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        return bin(n).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        # 使用与操作\n",
    "        res = 0\n",
    "        # while n: # 时间复杂度：O(log2n)  n指的是最高位的1所在的位置, log2 16 = 4  n=16，最高位1的位置为4\n",
    "        #     res += n & 1\n",
    "        #     n = n >> 1\n",
    "        # return res\n",
    "\n",
    "        # 巧用 n & n - 1\n",
    "        while n:\n",
    "            res += 1\n",
    "            n = n & n -1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n & 1\n",
    "            n >>= 1\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n != 0:\n",
    "            if n & 1:\n",
    "                res += 1\n",
    "            n >>= 1\n",
    "            \n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res=0\n",
    "        while n:\n",
    "            res+=n&1\n",
    "            n=n>>1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count = 0\n",
    "        while n:\n",
    "            n &= n - 1\n",
    "            count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n & 1\n",
    "            n >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        tmp = 1\n",
    "        count = 0\n",
    "        while tmp <= n:\n",
    "            if tmp & n != 0:\n",
    "                count += 1\n",
    "            tmp <<= 1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        j = 1\n",
    "        for i in range(32):\n",
    "            if n & j:\n",
    "                res += 1\n",
    "            j = j << 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "       \n",
    "        while n != 0:\n",
    "\n",
    "            res += n & 1\n",
    "            n >>= 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count = 0\n",
    "\n",
    "        while n != 0:\n",
    "            temp = n % 2\n",
    "            n = n // 2\n",
    "            if temp == 1:\n",
    "                count += 1\n",
    "        return count\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans=0\n",
    "        while n:\n",
    "            n-=(-n)&n\n",
    "            ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count= 0\n",
    "        for i in range(32):\n",
    "            if n&(1<<i):\n",
    "                count +=1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt=0\n",
    "        while n!=0:\n",
    "            n=n&(n-1)\n",
    "            cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n != 0:\n",
    "            if n %2 == 1:ans +=1\n",
    "            n //=2\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += 1\n",
    "            n &= n-1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n>0:\n",
    "            if n%2:res+=1\n",
    "            n = n // 2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n:\n",
    "            if n % 2 == 1:\n",
    "                ans += 1\n",
    "            n //= 2\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += 1\n",
    "            n &= n - 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        # bins = bin(n)[2:]\n",
    "        return bin(n)[2:].count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n > 0:\n",
    "            res += 1\n",
    "            n = n&(n-1)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        o=[]\n",
    "        total=0\n",
    "        while n >= 2:\n",
    "            o.append(n % 2)\n",
    "            n= n // 2\n",
    "        o.append(n)\n",
    "        for num in o:\n",
    "            if num == 1:\n",
    "                total+=1\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = sum(1 for i in range(32) if n & (1 << i)) \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        bin_num=bin(n).replace('0b','')\n",
    "        one_num=0\n",
    "        for i in range(0,len(bin_num)):\n",
    "            if bin_num[i]=='1':\n",
    "                one_num+=1\n",
    "        return one_num\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n&1\n",
    "            n >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n&1\n",
    "            n >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n:\n",
    "            ans+=n&1\n",
    "            n>>=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        num = 0\n",
    "        while n:\n",
    "            m = n % 2\n",
    "            n = n//2            \n",
    "            if m == 1:\n",
    "                num+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt=0\n",
    "        while n>0:\n",
    "            n&=n-1\n",
    "            cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = sum(1 for i in range(32) if n & (1 << i))\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        n = bin(n)[2:]\n",
    "        left = 0\n",
    "        right = len(n) - 1\n",
    "        result = 0\n",
    "        while left < right:\n",
    "            if n[left] == '1':\n",
    "                result += 1\n",
    "            if n[right] == '1':\n",
    "                result += 1\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        if left == right:\n",
    "            if n[left] == '1':\n",
    "                result += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n:\n",
    "            n -= n & -n\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count = 0\n",
    "        while n:\n",
    "            count += n & 1\n",
    "            n >>= 1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(32):\n",
    "            cnt+= (n>>i) & 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(31,-1, -1):    #倒序要写-1\n",
    "            if n // 2**i == 1:\n",
    "                n -= 2**i\n",
    "                ans += 1\n",
    "                # print(n)\n",
    "        return ans\n",
    "\n",
    "             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n):\n",
    "\n",
    "        strs = bin(n)[2:]\n",
    "        res = 0\n",
    "        for i in range(len(strs)):\n",
    "            if strs[i] == \"1\":\n",
    "                res += 1\n",
    "        return res \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ans=0\n",
    "        while n:\n",
    "            ans+=1\n",
    "            n&=n-1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res=0\n",
    "        while n:\n",
    "            res+=n&1\n",
    "            n>>=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        if n is not None:\n",
    "            ans = bin(n)\n",
    "            new_list = ans\n",
    "            count=0\n",
    "            for i in range(len(new_list)):\n",
    "                if new_list[i] == \"1\":\n",
    "                    count+=1\n",
    "            return count\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n != 0:\n",
    "            res += 1\n",
    "            n = n & (n-1)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        ret = sum(1 for i in range(32) if n & (1 << i))\n",
    "        return ret\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += 1\n",
    "            n &= n - 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res=0\n",
    "        while n:\n",
    "            res+= n&1 # 统计最后一位\n",
    "            n>>=1\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        count=0\n",
    "        while n:\n",
    "            count+=n&1\n",
    "            n>>=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n:\n",
    "            res += n & 1\n",
    "            n >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hammingWeight(self, n: int) -> int:\n",
    "        cnt=0\n",
    "        while n>0:\n",
    "            cnt+= n&1\n",
    "            n>>=1\n",
    "\n",
    "        return cnt"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
